var request = require('request')
var che = require('cheerio')
var opt = require('./creatHeaders')

function RequestId(id) {
  return new Promise((resolve, reject) => {
    request(opt(`https://github.com/search?q=${id}`), (err, res, body) => {
      if (err) {
        reject(new Error(err))
      }
      let array = []
      var $ = che.load(body)
      const ele = $('.repo-list-item.hx_hit-repo.d-flex.flex-justify-start.py-4.public.source')
      const l = ele.length
      for (let i = 0; i < l; i++) {
        array.push({
          title: ele.eq(i).find('.f4.text-normal').text()
        })
      }
      resolve(array)
    })
  })

}

module.exports = RequestId